<html>
<head>
    <title>学生信息添加</title>
    <link rel="stylesheet" th:href="@{/css/plugins/layui/css/layui.css}">
</head>
<body>
<div>

    <form class="layui-form" method="post" action="/student/add" lay-filter="formDemo">
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">学号</label>
                <div class="layui-input-inline layui-input-wrap">
                    <input type="text" id="studentNum" name="studentNum" placeholder="请输入学号" class="layui-input">
                </div>
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">姓名</label>
                <div class="layui-input-inline layui-input-wrap">
                    <input type="text" id="studentName" placeholder="请输入姓名" class="layui-input">
                </div>
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">性别</label>
                <div class="layui-input-inline layui-input-wrap">
                    <input type="radio"  name="gender" value="0" title="男" checked>
                    <input type="radio"  name="gender" value="1" title="女">
                    <input type="radio"  name="gender" value="2" title="保密">
                </div>
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">年龄</label>
                <div class="layui-input-inline layui-input-wrap">
                    <input type="number" id="age" placeholder="请输入年龄" class="layui-input">
                </div>
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">出生日期</label>
                <div class="layui-input-inline layui-input-wrap">
                    <input type="text" id="birthday" placeholder="请选择出生日期" class="layui-input">
                </div>
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">班级编号</label>
                <div class="layui-input-inline layui-input-wrap">
                    <select id="classId" th:with="classs=${@classService.list()}">
                        <option value="">请选择</option>
                        <option th:each="class : ${classs}" th:text="${class.className}" th:value="${class.classId}"></option>
                    </select>
                </div>
            </div>
        </div>


        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">宿舍编号</label>
                <div class="layui-input-inline layui-input-wrap">
                    <select id="hotelId" th:with="hotels=${@hotelService.list()}">
                        <option value="">请选择</option>
                        <option th:each="hotel : ${hotels}" th:text="${hotel.hotelName}" th:value="${hotel.hotelId}"></option>
                    </select>
                </div>
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">联系电话</label>
                <div class="layui-input-inline layui-input-wrap">
                    <input type="text" id="phone" placeholder="请输入联系电话" class="layui-input">
                </div>
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">电子邮箱地址</label>
                <div class="layui-input-inline layui-input-wrap">
                    <input type="email" id="email" placeholder="请输入电子邮箱地址" class="layui-input">
                </div>
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">家庭住址</label>
                <div class="layui-input-inline layui-input-wrap">
                    <input type="text" id="homeAddress" placeholder="请输入家庭住址" class="layui-input">
                </div>
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">紧急联系人姓名</label>
                <div class="layui-input-inline layui-input-wrap">
                    <input type="text" id="emergencyContact" placeholder="请输入紧急联系人姓名" class="layui-input">
                </div>
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">紧急联系人联系电话</label>
                <div class="layui-input-inline layui-input-wrap">
                    <input type="text" id="emergencyContactPhone" placeholder="请输入紧急联系人联系电话" class="layui-input">
                </div>
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">专业</label>
                <div class="layui-input-inline layui-input-wrap">
                    <select id="major" th:with="majors=${@majorService.list()}">
                        <option value="">请选择</option>
                        <option th:each="major : ${majors}" th:text="${major.major}" th:value="${major.major}"></option>
                    </select>
                </div>
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">学生状态</label>
                <div class="layui-input-inline layui-input-wrap">
                    <input type="radio" name="status" value="0" title="在读" checked>
                    <input type="radio" name="status" value="1" title="毕业">
                </div>
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">创建者</label>
                <div class="layui-input-inline layui-input-wrap">
                    <input type="text" id="createBy" name="createBy" placeholder="请输入创建者" class="layui-input">
                </div>
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">备注</label>
                <div class="layui-input-inline layui-input-wrap">
                    <input type="text" id="remark" placeholder="请输入备注" class="layui-input">
                </div>
            </div>
        </div>
    </form>
</div>

<script th:src="@{/js/jquery.min.js}"></script>
<script th:src="@{/js/plugins/layui/layui.js}"></script>
<script>
    layui.use(['form', 'laydate'], function () {
        var form = layui.form;
        var laydate = layui.laydate;
        var $ = layui.$;

        laydate.render({
            elem: '#birthday',
            type: 'date'
        });

        form.on('submit(formDemo)', function (data) {
            $.ajax({
                url: '/student/add', // 确保 URL 与表单的 action 属性一致
                type: 'post',
                data: data.field,
                dataType: 'json',
                success: function (res) {
                    if (res.code === 0) {
                        layer.msg('添加成功', {icon: 1});
                        parent.layer.closeAll(); // 关闭所有弹窗
                        parent.table.reload('test'); // 重新加载父页面的表格数据
                    } else {
                        layer.msg(res.msg, {icon: 2});
                    }
                },
                error: function () {
                    layer.msg('系统错误', {icon: 2});
                }
            });
            return false; // 阻止表单默认提交
        });
    });
</script>
</body>
</html>
